﻿using System.Diagnostics;
using MvvmFx.Common.ViewModels;
using MvvmFx.Common.ViewModels.Commands;

namespace MvvmFx.Samples.HowTos.ChangeTracking.ViewModels
{
    /// <summary>
    /// Command to save the process
    /// </summary>
    public class SaveCommand : ViewModelCommand
    {
        private readonly ContactDetailsViewModel _contactDetailsVM;

        public SaveCommand(ContactDetailsViewModel contactDetailsVM)
        {
            _contactDetailsVM = contactDetailsVM;

            //_contactDetailsVM.ChangeService.NewPropertyObserver()
            //                               .AddHandler("IsModified", Tester);
        }

        protected void Tester(ViewModelBase vm)
        {
            Debug.WriteLine("Tester - IsModifiedCHanged: " + _contactDetailsVM.ChangeService.IsModified);
            this.OnCanExecuteChanged();
        }

        protected override void OnExecute(object param)
        {
            _contactDetailsVM.Save();

            OnCanExecuteChanged();
        }

        protected override bool OnCanExecute(object param)
        {
            return _contactDetailsVM.ChangeService.IsModified;
        }
    }
}